package com.xiamo.utils;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class MailSender {
    public static void main(String[] args) {
        // 邮件服务器的配置信息
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.qq.com"); // 邮件服务器主机名
        properties.put("mail.smtp.port", "587"); // 邮件服务器端口号
        properties.put("mail.smtp.auth", "true"); // 启用身份验证

        // 邮件发送者的账号和密码
        final String username = "1375490651@qq.com";
        final String password = "tkrwoyaqvbsajbgc";

        // 创建会话对象
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            // 创建邮件消息
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("1375490651@qq.com")); // 发件人邮箱
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("838610032@qq.com")); // 收件人邮箱
            message.setSubject("Test Email"); // 邮件主题
            message.setText("This is a test email"); // 邮件内容

            // 发送邮件
            Transport transport = session.getTransport("smtp");
            transport.connect();
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

            System.out.println("Email sent successfully");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
